import 'package:flutter/material.dart';
import 'package:flutterapp/config.dart';
import 'package:flutterapp/models/room_app_liance_item.dart';
import 'package:flutterapp/page/widgets/common_check_button.dart';

const List<RoomAppLianceItem> _dataList = [
  const RoomAppLianceItem('衣柜', 0xe61f, false),
  const RoomAppLianceItem('洗衣机', 0xe90d, false),
  const RoomAppLianceItem('空调', 0xe60a, false),
  const RoomAppLianceItem('天然气', 0xe60e, false),
  const RoomAppLianceItem('冰箱', 0xec4f, false),
  const RoomAppLianceItem('暖气', 0xe67d, false),
  const RoomAppLianceItem('电视', 0xf0099, false),
  const RoomAppLianceItem('热水器', 0xe653, false),
  const RoomAppLianceItem('宽带', 0xe7e0, false),
  const RoomAppLianceItem('沙发', 0xe6c6, false),
];

//组件参数
//final ValueChanged<List<RoomAppLianceItem>> onChange;

class RoomAppLiance extends StatefulWidget {
  final ValueChanged<List<RoomAppLianceItem>> onChange;

  const RoomAppLiance({Key key, this.onChange}) : super(key: key);

  @override
  _RoomAppLianceState createState() => _RoomAppLianceState();
}

class _RoomAppLianceState extends State<RoomAppLiance> {
  List<RoomAppLianceItem> list = _dataList;
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Wrap(
        children: list
            .map((item) => GestureDetector(
                  //空白区域可以点击
                  behavior: HitTestBehavior.translucent,
                  onTap: () {
                    setState(() {
                      list = list
                          .map((inititem) => inititem == item
                              ? RoomAppLianceItem(
                                  item.title, item.iconPoint, !item.isChecked)
                              : inititem)
                          .toList();
                      //发送到父组件
                      if (null != widget.onChange) {
                        widget.onChange(list);
                        //print(list);
                      }
                    });
                  },
                  child: Container(
                    //宽度是屏幕宽度的5/1
                    width: MediaQuery.of(context).size.width / 5,
                    child: Column(
                      children: [
                        Padding(
                          padding: EdgeInsets.only(top: 9.0),
                        ),
                        Icon(
                            IconData(item.iconPoint,
                                fontFamily: Config.CommonIcon),
                            size: 40.0),
                        Padding(
                          padding: EdgeInsets.only(top: 5.0),
                        ),
                        Text(item.title),
                        CommonCheckButton(item.isChecked)
                      ],
                    ),
                  ),
                ))
            .toList(),
      ),
    );
  }
}

class RoomApplicanceList extends StatelessWidget {
  final List<String> list;

  const RoomApplicanceList({Key key, this.list}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (list == null) return Container();
    //过滤并获取过滤后的数组
    var showList =
        _dataList.where((element) => list.contains(element.title)).toList();
    return Container(
      child: Wrap(
        children: showList
            .map((item) => Container(
                  //宽度是屏幕宽度的5/1
                  width: MediaQuery.of(context).size.width / 5,
                  child: Column(
                    children: [
                      Padding(
                        padding: EdgeInsets.only(top: 9.0),
                      ),
                      Icon(
                          IconData(item.iconPoint,
                              fontFamily: Config.CommonIcon),
                          size: 40.0),
                      Padding(
                        padding: EdgeInsets.only(top: 5.0),
                      ),
                      Text(item.title),
                    ],
                  ),
                ))
            .toList(),
      ),
    );
  }
}
